from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):  # 必须继承这个类
    mobile_tel = models.CharField(max_length=32)


# 出版社
class Publish(models.Model):
    id = models.AutoField(primary_key=True)  # 主键
    name = models.CharField(max_length=32)  # 出版社
    city = models.CharField(max_length=64)  # 地址

    def __str__(self):
        return self.name


# 作者表
class Author(models.Model):
    id = models.AutoField(primary_key=True)  # 主键
    name = models.CharField(max_length=32)  # 姓名
    author_detail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)

    def __str__(self):
        return self.name


# 作者详细
class AuthorDetail(models.Model):
    id = models.AutoField(primary_key=True)  # 主键
    sex_choices = ((0, "女"), (1, "男"),)  # 性别选择
    sex = models.SmallIntegerField(choices=sex_choices)  # 性别
    mobile_tel = models.CharField(max_length=32)  # 手机号码
    email = models.EmailField()  # 邮箱格式
    addr = models.CharField(max_length=64)  # 联系地址


# 书籍表
class Book(models.Model):
    id = models.AutoField(primary_key=True)  # 主键
    title = models.CharField(max_length=32)  # 书名
    price = models.DecimalField(max_digits=8, decimal_places=2)  # 价格 小数保留后2位
    pub_date = models.DateField()  # 出版日期 日期格式
    publish = models.ForeignKey("Publish", on_delete=models.CASCADE)  # 出版社 # 出版社和书是一对多 在多的表上关联,
    authors = models.ManyToManyField("Author")  # 作者 # 书跟作者是多对多的关系 用Django语句可以将书籍表与作者表关联起来,自己会创建个表

    def __str__(self):
        return self.title

